#include<stdio.h>
#include<malloc.h>

int tl=0;
//void index(int);

//////////////////////////////////////////////////////////////
/*struct node1
{
int data;
struct node1 *next;
}*/
///////////////////////////////////////////////////////////////
struct node
{
int data;
struct node *next;
} *start= NULL;

struct node *new_node, *current;
/////////////////////////////////////////////////////////////
void newlist() // to create a linked list (1)
{
char ch1='y';
while(ch1=='y')
{

new_node=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data :");

scanf("%d",&new_node->data);
tl++;
new_node->next=NULL;

if(start==NULL)
{
start=new_node;
current=new_node;
}
else
{
current->next=new_node;
current=new_node;
}
printf("\n do u want to create another: ");
//scanf("%c",&ch1);
scanf("%s",&ch1);
}
struct node *a;
a=start;
while(a->next!=NULL)
{printf("%d ->",a->data);
a = a->next;
}
printf(" %d -> ",a->data);
printf("\n\n");
}
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
void index1() // to search an index of an element (5)
{
int n=0,m=0;
new_node=start;
int item;
 
printf("enter the no. to be searched : ");
scanf("%d",&item);

while(new_node!=NULL)
{
if(new_node->data==item)
{

printf("the value is at  %d  ",n);
m++;
break;
}

else
{n++;
}
new_node=new_node->next;
}
if(m==0)
printf("the value is not there");

}
///////////////////////////////////////////////////////

///////////////////////////////////////////////////////
void count1() // function to count total elements (9)
{ int cnt=0;
new_node=start;
int n,i;
printf("enter the element whose no. of occurences u want ");
scanf("%d",&n);

while(new_node->next!=NULL)
{ if(new_node->data==n)
cnt++;
new_node=new_node->next;
}

if(new_node->data==n)
{cnt++;
printf("the total number of nodes are : %d", cnt);
}

else
{
printf("the total number of nodes are : %d", cnt);
}
}

//////////////////////////////////////////////////////
/*
void count1()
{ int n,i;
int count=0;
printf("enter the element whose no. of occurences u want ");
scanf("%d",&n);

for(i=0;i<tl;i++)
{if(a[i]==n)
{count++;
}
}
printf("the no. of times  %d  ", n);
printf("is occuring is %d  ",count);
}
///////////////////////////////////////////////////////

*/
///////////////////////////////////////////////////////
void append()
{int num;int i;
printf("enter the element you want to append  ");
scanf("%d",&num);
tl++;
struct node *t;
t=(struct node *)calloc(1,sizeof(struct node));
while(new_node->next!=NULL)
{
new_node=new_node->next;
}

new_node->next=t;
t->data=num;
t->next=NULL;

struct node *a;
a=start;
while(a->next!=NULL)
{printf("%d ->",a->data);
a = a->next;
}
printf(" %d -> ",a->data);
printf("\n\n");

}
///////////////////////////////////////////////////////////////
void extend()
{int kl=0;
char ch1='y';
struct node *nn,*s=NULL,*c;
while(ch1=='y')
{
nn=(struct node *)malloc(sizeof(struct node));
printf("\n enter the data :");
scanf("%d",&nn->data);
kl++;
nn->next=NULL;

if(s==NULL)
{
s=nn;
c=nn;
}
else
{
c->next=nn;
c=nn;
}

printf("\n do u want to create another: ");
//scanf("%c",&ch1);
scanf("%s",&ch1);
}
tl=tl+kl;
struct node *a1;
a1=s;
while(a1->next!=NULL)
{printf("%d ->",a1->data);
a1 = a1->next;
}
printf(" %d -> ",a1->data);
printf("\n\n");

new_node=start;

while(new_node->next!=NULL)
{new_node=new_node->next;}
new_node->next=s;


struct node *a2;
a2=start;
while(a2->next!=NULL)
{printf("%d ->",a2->data);
a2 = a2->next;
}
printf(" %d -> ",a2->data);
printf("\n\n");
}
/*void extend()
{ int ell=0;
char c='k';
int *b;
//int total;
printf(" enter the new list which is to be extended\n\n");
printf("start entering nos.separated by comma  (end wid a dot)\n\n");
while(c!='.')
{
b=(int*)realloc(b,(ell+1)*sizeof(int*));
scanf("%d",&b[ell]);
scanf("%c",&c);
ell++;
};
int i;
printf("\n\nthe new list is    ");
for(i=0;i<ell;i++)
{
printf("  %d -> ",b[i]);
}
a=(int*)realloc(a,(tl+ell)*sizeof(int*));

for(i=0;i<ell;i++)
{a[tl+i]=b[i];
}

tl=tl+ell;

printf(" \n\n the extended list is\n  ");
for(i=0;i<tl;i++)
{ printf("%d -> ",a[i]);}
}

*/
//////////////////////////////////////////////////////////////
void insert1()
{ 
struct node *tmp,*k,*l,*k1;
int i,ij;

tmp=(struct node *)calloc(1,sizeof(struct node));

printf("enter the no. you want to insert");
scanf("%d",&tmp->data);
printf("enter the index where u want to insert this no.");
scanf("%d",&i);
//k1=start;
if(i>tl+1){printf("invalid position");}

else if(i==0)
{
tmp->next=start;
struct node *a;
a=tmp;
while(a->next!=NULL)
{printf("%d ->",a->data);
a = a->next;
}
printf(" %d -> ",a->data);
printf("\n\n");
}

else if(i=tl+1)
{new_node=start;
while(new_node->next!=NULL)
{
new_node=new_node->next;
}

new_node->next=tmp;
//tmp->data=num;
tmp->next=NULL;

struct node *a;
a=start;
while(a->next!=NULL)
{printf("%d ->",a->data);
a = a->next;
}
printf(" %d -> ",a->data);
printf("\n\n");

}

else 
{
k=start;
l=start;

for(ij=1;ij<=i;ij++)
{k=k->next;}
tmp->next=k;

for(ij=1;ij<i;ij++)
{l=l->next;}
l->next=tmp;


struct node *a;
a=start;
while(a->next!=NULL)
{printf("%d ->",a->data);
a = a->next;
}
printf(" %d -> ",a->data);
printf("\n\n");
}
}

////////////////////////////////////////////////////////////////
void remove1()
{int num=0,k=0;
//struct node *k1;
printf("enter the no. to be removed");
scanf("%d",&num);

new_node=start;
while(new_node->next!=NULL)
{
if(new_node->next->data=num)
{k++;
break;}
}

if (k!=0)
{new_node->next=new_node->next->next;}
else
printf("no not there");

struct node *a;
a=start;
while(a->next!=NULL)
{printf("%d ->",a->data);
a = a->next;
}
printf(" %d -> ",a->data);
printf("\n\n");

}

////////////////////////////////////////////////////////////////
void pop1()
{int n,i;
printf("enter the index from where u want to delete");
scanf("%d",&n);


struct node *tmp;
new_node=start;

if(n==0)
{
tmp=new_node;
new_node=new_node->next;
free(tmp);

struct node *a;
a=new_node;
while(a->next!=NULL)
{printf("%d ->",a->data);
a = a->next;
}
printf(" %d -> ",a->data);
printf("\n\n");
}

else
{
for(i=0;i<n-1;i++)
{new_node=new_node->next;
}
tmp=new_node->next;
new_node->next=new_node->next->next;
free(tmp);

struct node *a;
a=start;
while(a->next!=NULL)
{printf("%d ->",a->data);
a = a->next;
}
printf(" %d -> ",a->data);
printf("\n\n");
}
}


///////////////////////////////////////////////////////////////////
void reverse()
{ int k=0;

new_node=start;
while(new_node!=NULL)
{new_node=new_node->next;
k++;}
//k=k--;
struct node *a[k];
int i;
new_node=start;

for(i=k;i>0;i--)
{a[i]=new_node->data;
//printf(" %d  -> ",a[i]);
new_node=new_node->next;}

printf("reversed list is");
for(i=1;i<=k;i++)
{printf(" %d  -> ",a[i]);
}
}
//////////////////////////////////////////////////////////////
void sort()
{ int k=0;
new_node=start;
while(new_node!=NULL)
{new_node=new_node->next;
k++;}
 int i,j,tmp;
int a[k];
for(i=0;i<k;i++)
{ a[i]=new_node->data;
new_node=new_node->next;}


for(i=0;i<k-1;i++)
{
for(j=0;j<k-1;j++)
{
if(a[j]>a[j+1])
{
tmp=a[j];
a[j]=a[j+1];
a[j+1]=tmp;
}}}

	printf("the sorted list is ");
	for(i=0;i<k;i++)
	{ printf("%d -> ", a[i]);
	}
}

//////////////////////////////////////////////////////////////
/*
void insert1()
{ int n,i,j,tmp;
printf("enter the no. you want to insert");
scanf("%d",&n);
printf("enter the index where u want to insert this no.");
scanf("%d",&i);

tl=tl+1;

for(j=tl;j>i;j--)
{a[j]=a[j-1];
}
a[i]=n;

printf(" the list after inserting %d  at %d postion is \n\n",n,i);

for(j=0;j<tl;j++)
{
printf("%d -> ",a[j]);
}
}

void append()
{int num;
int i;
 printf("enter the element you want to append  ");
scanf("%d",&a[tl]);
tl=tl+1;
printf(" the new list is  ");
	for(i=0;i<tl;i++)
	{ printf("%d -> ", a[i]);
	}
}*/
///////////////////////////////////////////////////////
/*struct node *pop1( /*struct node *list, int no) // to delete a no. (4)
{
struct node *cur, *prev,*list;

printf("enter the number to be poped");

list=start;

for(cur=list,prev=NULL;cur!=NULL && cur->value!=no; prev=cur, cur=cur->next);

if(cur==NULL) //no. was not there in list
return list;

if (prev==NULL) // no. is in first node
list=list->next;
else //no is some other node
prev=prev->next;

free(cur);
return list;



}
*/
////////////////////////////////////////////////////////

void main()
{char choice ='y';
int ch,item,pos;

while(choice=='y')
{
printf("\n\n\t\tWELCOME TO THE LIST WORLD \n");
printf("\n\t\tENJOY THE FOLLOWING OPTIONS \n\n");
printf("\t\t1.MAKE A LIST \n"); ///////////////////////////
printf("\t\t2.SORTING \n");
printf("\t\t3.APPEND A NEW NUMBER \n");//////////////////////
printf("\t\t4.EXTEND THE LIST\n");/////////////////////////////
printf("\t\t5.INDEX OF AN ELEMENT \n");/////////////////////////////
printf("\t\t6.POP A NO. \n");
printf("\t\t7.REVERSE THE LIST \n");
printf("\t\t8.REMOVE AN ELEMENT \n");
printf("\t\t9.COUNT\n");/////////////////////
printf("\t\t10.INSERT A NO. AT GIVEN POSTION\n\n\n");///////////////////////

printf("ENTER CHOICE YOU WANT TO GO FOR");
scanf("%d",&ch);

if(ch==1)
{ newlist(); } //to make a new list

else if(ch==2)
{sort();} // to sort the entered list

else if(ch==3)
{append();} // to insert a new element in the list

else if(ch==4)
{extend();} // to insert a new list in the list

else if(ch==5)
{index1();} // to get an index of an element in the list

else if(ch==6)
{pop1();} // to remove the element at a particular index in the list

else if(ch==7)
{reverse();} // to reverse the list

else if(ch==8)
{remove1();} // to remove the first occurence of the particular element

else if(ch==9)
{count1();} // to count the occurences of a particular element

else if(ch==10)
{insert1();} // to insert a particular no. at particular position

fflush(stdin);
printf("\n\n\n\n DO YOU WANT TO DO MORE FUNCTIONS (y/n)  \n\n ");
scanf("%s",&choice);
}
}

